package com.miraksoft.statewatcher.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import android.widget.Button;
import android.widget.TextView;

public class WatchedObjectStateRuntime implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2105395965271337562L;
	
	private WatchedObjectRuntime parentWatchedObjectRuntime = null;
	private WatchedObjectStateDef watchedObjectStateDef = null;
	private Button button;
	private TextView textViewTimer;
	private List<StateEvent> events = new ArrayList<StateEvent>();
	
	// measured time in [ms]
	private Long timer = 0L ;
	private STATE state = STATE.STOPPED;
	
	enum STATE {
		RUNNING,
		PAUSED,
		STOPPED
	}
	
	public WatchedObjectStateRuntime (WatchedObjectStateDef watchedObjectStateDef){
		this.watchedObjectStateDef = watchedObjectStateDef;
	}
	
	public WatchedObjectStateDef getWatchedObjectStateDef() {
		return watchedObjectStateDef;
	}
	
	public void setWatchedObjectStateDef(WatchedObjectStateDef state) {
		this.watchedObjectStateDef = state;
	}
	
	public List<StateEvent> getEvents() {
		return events;
	}
	
	public void setEvents(List<StateEvent> events) {
		this.events = events;
	}

	public Button getButton() {
		return button;
	}

	public void setButton(Button button) {
		this.button = button;
	}

	public WatchedObjectRuntime getParentWatchedObjectRuntime() {
		return parentWatchedObjectRuntime;
	}

	public void setParentWatchedObjectRuntime(
			WatchedObjectRuntime parentWatchedObjectRuntime) {
		this.parentWatchedObjectRuntime = parentWatchedObjectRuntime;
	}

	public void setTextTimer(TextView textViewTimer) {
		this.textViewTimer = textViewTimer;
	}

	public boolean isDefault(){
		return watchedObjectStateDef.isDefault();
	}
	
	public void start(){
		timer = 0L;
		state = STATE.RUNNING;
	}

	public void pause(){
		state = STATE.PAUSED;
	}

	public void stop(){
		state = STATE.STOPPED;
	}

	public void resume(){
		state = STATE.RUNNING;
	}

	public void reset(){
		timer = 0L;
		state = STATE.STOPPED;
	}
	
	public void tick(){
		timer++;
		if( timer < 60*60*24 ){
			textViewTimer.setText( String.format( "%02d:%02d", (int) ((timer/60) % 24), (int) (timer % 60)) );
		} else {
			textViewTimer.setText( String.format( "%d:%02d:%02d", (int) ((timer/60*60) % 24), (int) ((timer / 60) % 24), (int) (timer % 60)) );
		}
	}
	
	public boolean isRunning() { return STATE.RUNNING.equals(state); }
	public boolean isPaused()  { return STATE.PAUSED.equals(state); }
	public boolean isStopped() { return STATE.STOPPED.equals(state); }
	
	public String toString(){
		return 
			"parentWatchedObjectRuntime: " + parentWatchedObjectRuntime.getWatchedObjectDef().getName() + "\n" + 
			"watchedObjectStateDef: " +  watchedObjectStateDef.getName() + "\n" + 
			"state: " +  state.toString() + "\n" + 
			"button: " + button.getId() + "\n" + 
			"timer: " + timer + "\n" + 
			"events: " + events;

	}
}
